﻿using HighThroughput.Common;
using NPOI.DDF;
using OSSUtility;
using Ozone.BLL;
using Ozone.Common;
using Ozone.Help;
using Ozone.Models;
using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Ozone.Views.Widget
{
    /// <summary>
    /// WindowLogin1.xaml 的交互逻辑
    /// </summary>
    public partial class WindowLogin : Window
    {
        private string userCode = null;
        private string userPassword = null;

        public WindowLogin()
        {
            InitializeComponent();
            LoadSavedCredentials();
        }

        private void ButtonExit_Click(object sender, RoutedEventArgs e)
        {
            //WindowConfirmationDialog confirmationDialog = new WindowConfirmationDialog("确定要退出吗？");
            //if (confirmationDialog.ShowDialog() == true)
            //{
            //    globalVar.currentSys_UserModel = null;
            //}
            DialogResult = false;
            this.Close();
        }

        private void ButtonLogin_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtUserCode.Text))
            {
                HighThroughput.Common.Tools.ShowPromptDialog(InfoEnum.DialogType.Warning.ToString(), "请输入登录账号！");

                txtUserCode.Focus();
                return;
            }
            if (string.IsNullOrWhiteSpace(pbUserPassword.Password))
            {
                HighThroughput.Common.Tools.ShowPromptDialog(InfoEnum.DialogType.Warning.ToString(), "请输入密码！");
                pbUserPassword.Focus();
                return;
            }
            userCode = txtUserCode.Text;
            userPassword = pbUserPassword.Password;
            List<sys_user> sys_UsersModelList = Sys_UserBLL.GetUserInfoList(userCode, userPassword);
            if (sys_UsersModelList != null)
            {
                if (sys_UsersModelList.Count > 0 && sys_UsersModelList.Count == 1)
                {
                    globalVar.currentSys_UserModel = sys_UsersModelList[0];
                    //HighThroughput.Common.Tools.ShowPromptDialog(InfoEnum.DialogType.Info.ToString(), "登录成功！");
                    if (chkRember.IsChecked == true)
                    {
                        // 保存凭据
                        if (chkRember.IsChecked == true)
                        {
                            SaveCredentials(userCode, userPassword);
                        }
                        else
                        {
                            Tools.ClearCredentials();
                        }
                    }
                    DialogResult = true;
                    this.Close();
                }
                else
                {
                    HighThroughput.Common.Tools.ShowPromptDialog(InfoEnum.DialogType.Error.ToString(), "用户编码或密码错误，请检查！");
                }
            }
            else
            {
                HighThroughput.Common.Tools.ShowPromptDialog(InfoEnum.DialogType.Error.ToString(), "登录失败,请检查用户名密码！");
            }
        }

        // 加载保存的凭据
        private void LoadSavedCredentials()
        {
            try
            {
                using (var store = IsolatedStorageFile.GetUserStoreForAssembly())
                {
                    if (store.FileExists("credentials.txt"))
                    {
                        using (var stream = new IsolatedStorageFileStream("credentials.txt", System.IO.FileMode.Open, store))
                        using (var reader = new System.IO.StreamReader(stream))
                        {
                            txtUserCode.Text = reader.ReadLine();
                            pbUserPassword.Password = reader.ReadLine();
                            chkRember.IsChecked = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("加载凭据失败", ex);
            }
        }

        // 保存凭据到本地存储
        private void SaveCredentials(string userCode, string password)
        {
            try
            {
                using (var store = IsolatedStorageFile.GetUserStoreForAssembly())
                {
                    using (var stream = new IsolatedStorageFileStream("credentials.txt", System.IO.FileMode.Create, store))
                    using (var writer = new System.IO.StreamWriter(stream))
                    {
                        writer.WriteLine(userCode);
                        writer.WriteLine(password);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("保存凭据失败", ex);
            }
        }
    }
}